package com.tjsinfo.tjsoa.worklog.bo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.ISystemUserDao;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.worklog.dao.IWorkLogDefineDao;
import com.tjsinfo.tjsoa.worklog.dao.IibatisWorkLogDefineDao;
import com.tjsinfo.tjsoa.worklog.vo.WorkLogDefine;
import com.tjsinfo.tjsoa.worklog.vo.WorkLogExamine;
import com.tjsinfo.tjsoa.worklog.vo.WorkLogMain;
public class WorkLogDefineBo implements IWorkLogDefineBo{
	private  final  boolean  starttrue = true;
	private  final  boolean  starfalse = false;
	IWorkLogDefineDao workLogDefineDao;
	private IibatisWorkLogDefineDao iibatisWorkLogDefineDao;
	private ISystemUserDao systemUserDao;
	private IQbcDao qbcDao;
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public ISystemUserDao getSystemUserDao() {
		return systemUserDao;
	}

	public void setSystemUserDao(ISystemUserDao systemUserDao) {
		this.systemUserDao = systemUserDao;
	}

	public IibatisWorkLogDefineDao getIibatisWorkLogDefineDao() {
		return iibatisWorkLogDefineDao;
	}

	public void setIibatisWorkLogDefineDao(
			IibatisWorkLogDefineDao iibatisWorkLogDefineDao) {
		this.iibatisWorkLogDefineDao = iibatisWorkLogDefineDao;
	}

	public IWorkLogDefineDao getWorkLogDefineDao() {
		return workLogDefineDao;
	}

	public void setWorkLogDefineDao(IWorkLogDefineDao workLogDefineDao) {
		this.workLogDefineDao = workLogDefineDao;
	}

	public boolean addDefine(User user, String[] userarray, String type) {
		// TODO Auto-generated method stub
		try {
			
			List<WorkLogDefine> list = new ArrayList<WorkLogDefine>();//工作日志缺省列表
			if(userarray != null)
			{
				for (String str : userarray) {
					WorkLogDefine define = new WorkLogDefine();
					TSystemUser tjsUser = new TSystemUser();
					tjsUser.setUserId(Integer.parseInt(str));
					define.setTjsUser(tjsUser);
					define.setUserId(user.getUser_id());
					
					define.setType(type);
					list.add(define);
				}
			}
			
			com.tjsinfo.tjsoa.worklog.sqlmap.WorkLogDefine workLogDefine = new com.tjsinfo.tjsoa.worklog.sqlmap.WorkLogDefine();
			workLogDefine.setTjsUserByUserId(user.getUser_id());
			workLogDefine.setType(type);
			iibatisWorkLogDefineDao.delDefintlist(workLogDefine);
			if(list.size() > 0)
			{
				workLogDefineDao.addDefintlist(list);
			}
			
		return starttrue;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return starfalse;
		}
	}

	public void delete(WorkLogDefine workLogDefine) {
		// TODO Auto-generated method stub
		workLogDefineDao.delete(workLogDefine);
	}

	public String save(WorkLogDefine workLogDefine) {
		// TODO Auto-generated method stub
		return workLogDefineDao.save(workLogDefine);
	}

	public WorkLogDefine selectDefineAndType(String type, Integer userid) {
		// TODO Auto-generated method stub
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		WorkLogDefine logDefine = new WorkLogDefine();//工作日志缺省定义
		logDefine.setTjsUser(tjsUser);
		logDefine.setType(type);
		
		return 	 workLogDefineDao.selectDefineAndType(logDefine);
	}

	public List selectDefineAndType(Integer userid) {
		// TODO Auto-generated method stub
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		WorkLogDefine logDefine = new WorkLogDefine();
		logDefine.setTjsUser(tjsUser);
		List list2 = new ArrayList(); 	
		List list =	workLogDefineDao.selectUserNoType(logDefine);
		Set<WorkLogDefine> set = new HashSet<WorkLogDefine>(list);
		list.clear();
		for (Iterator iterator = set.iterator(); iterator.hasNext();) {
			WorkLogDefine workLogDefine = (WorkLogDefine) iterator.next();
			list.add(workLogDefine);
		}
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkLogDefine workLogDefine = (WorkLogDefine) iterator.next();
			workLogDefine.setTjsUserName(systemUserDao.findById(workLogDefine.getUserId()).getUserName()); 
			list2.add(workLogDefine);
		}
		return list2;
	}

	public void update(WorkLogDefine workLogDefine) {
		// TODO Auto-generated method stub
		workLogDefineDao.update(workLogDefine);
	}

	/**
	 * 查询当前人的默认提交者

	 * @param 当前用户
	 * @return
	 */
	public List<WorkLogExamine> selectDefineAndTypeList(Integer userid)
	{
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		WorkLogDefine logDefine = new WorkLogDefine();
		logDefine.setTjsUser(tjsUser);
		return 	workLogDefineDao.selectDefineUserNoType(logDefine);
		
	}
	
	/**
	 * 查询当前人的可以审批的人
	 * @param 当前用户
	 * @return
	 */
	public List selectDefineUserList(Integer userid)
	{
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		WorkLogDefine logDefine = new WorkLogDefine();
		logDefine.setTjsUser(tjsUser);
		return 	workLogDefineDao.selectDefineUser(logDefine);
		
	}
	
	
	public List selectDefine(Integer userid) {
		// TODO Auto-generated method stub
		DetachedCriteria detachedCriteria  = DetachedCriteria.forClass(WorkLogDefine.class);
		detachedCriteria.add(Restrictions.eq("userId", userid));
		List<WorkLogDefine> list = qbcDao.getCriteria(detachedCriteria);
		ArrayList<TSystemUser> userlist = new ArrayList<TSystemUser>();
		for (WorkLogDefine workLogDefine : list) {
			DetachedCriteria criteria  = DetachedCriteria.forClass(TSystemUser.class);
			criteria.add(Restrictions.eq("userId", workLogDefine.getTjsUser().getUserId()));
			TSystemUser systemUser = (TSystemUser) qbcDao.getCriteria(criteria).get(0);
			userlist.add(systemUser);
		}
		return userlist; 
	}

}
